class ListNode(object):
    def __init__(self, x):
        self.val = x
        self.next = None


class Solution(object):
    def detectCycle(self, head):
        """
        :type head: ListNode
        :rtype: ListNode
        """
        if not head:
            return None
        max_value = 10**5
        add_value = 2 * 10**5 + 1
        flag = False
        node = head
        while node:
            if node.val > max_value:
                flag = True
                break
            else:
                node.val += add_value
                node = node.next
        if not flag:
            return None
        else:
            while head.val > max_value:
                head.val -= add_value
                head = head.next
            return head

